<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_industry".
 *
 * @property string $id
 * @property string $parent_id
 * @property string $title
 * @property integer $sort_order
 */
class MktIndustry extends \common\wosotech\base\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_industry';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent_id', 'sort_order'], 'integer'],       // no use now
            [['title'], 'string', 'max' => 256]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'parent_id' => '上级行业ID',
            'title' => '名称',
            'sort_order' => '显示顺序',
        ];
    }

    /**
     * @inheritdoc
     * @return MktIndustryQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktIndustryQuery(get_called_class());
    }

    public function behaviors() {
        return [
            [
                'class' => \common\wosotech\adjacencyList\AdjacencyListBehavior::className(),
                'parentAttribute' => 'parent_id',
                'sortable' => [
                    'sortAttribute' => 'sort_order',
                ],
            ],
        ];
    }

    public function getPaths($sep = '', $includeSelf = true)
    {
        $parents = $this->parents;
        if ($includeSelf) {
            $parents[] = $this;
        }
        $titles = yii\helpers\ArrayHelper::getColumn($parents, 'title');        
        return implode($sep, $titles);
    }

    // 得到某子目录的一级目录
    public function getRoot()
    {
        $parents = $this->parents;
        return $parents[0];
    }

}
